package com.lhy.multipledatasources.service;// UserService.java

import com.lhy.multipledatasources.entity.User;
import com.lhy.multipledatasources.mapper.primary.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {

    private final UserMapper userMapper;

    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    public List<User> getAllUsers() {
        return userMapper.selectAll();
    }

    @Transactional(transactionManager = "primaryTransactionManager")
    public int createUser(User user) {
        return userMapper.insert(user);
    }

    @Transactional(transactionManager = "primaryTransactionManager")
    public int updateUser(User user) {
        return userMapper.update(user);
    }

    @Transactional(transactionManager = "primaryTransactionManager")
    public int deleteUser(Long id) {
        return userMapper.delete(id);
    }
}